﻿using System;
using System.Collections;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Algorithm;
using YJKGridTopologyCal.GeometriesGraph;
using YJKGridTopologyCal.Utilities;

namespace YJKGridTopologyCal.Operation.Valid
{
	public class SimpleNestedRingTester
	{
		public SimpleNestedRingTester(GeometryGraph graph)
		{
			this.graph = graph;
		}

		public void Add(ILinearRing ring)
		{
			this.rings.Add(ring);
		}

		public Coordinate NestedPoint
		{
			get
			{
				return this.nestedPt;
			}
		}

		public bool IsNonNested()
		{
			for (int i = 0; i < this.rings.Count; i++)
			{
				ILinearRing linearRing = (ILinearRing)this.rings[i];
				Coordinate[] coordinates = linearRing.Coordinates;
				for (int j = 0; j < this.rings.Count; j++)
				{
					ILinearRing linearRing2 = (ILinearRing)this.rings[j];
					Coordinate[] coordinates2 = linearRing2.Coordinates;
					if (linearRing != linearRing2 && linearRing.EnvelopeInternal.Intersects(linearRing2.EnvelopeInternal))
					{
						Coordinate coordinate = IsValidOp.FindPointNotNode(coordinates, linearRing2, this.graph);
						Assert.IsTrue(coordinate != null, "Unable to find a ring point not a node of the search ring");
						if (CGAlgorithms.IsPointInRing(coordinate, coordinates2))
						{
							this.nestedPt = coordinate;
							return false;
						}
					}
				}
			}
			return true;
		}

		private readonly GeometryGraph graph;

		private readonly IList rings = new ArrayList();

		private Coordinate nestedPt;
	}
}
